<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加管理员
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="ID"
        prop="id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="用户名"
        prop="user_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="昵称"
        prop="nick_name"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <div v-if="row.nick_name">{{ row.nick_name }}</div>
          <div v-else>---</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="真实姓名"
        prop="true_name"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <div v-if="row.true_name">{{ row.true_name }}</div>
          <div v-else>---</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="手机号"
        prop="user_mobile"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="所属部门" prop="department_name">
        <template #default="{ row }">
          <div v-if="row.department_name">{{ row.department_name }}</div>
          <div v-else>暂无部门</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="角色"
        prop="role_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="登录次数"
        prop="login_num"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="最后登录IP"
        prop="last_login_ip"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="最后登录时间"
        prop="last_login_time_format"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="操作" width="185">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">修改权限</el-button>
          <el-button type="text" @click="handleDelete(row)">
            取消管理员权限
          </el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.perpage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <member-add ref="add" @fetch-data="fetchData" />
    <member-edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, delAdmin } from '@/api/admin'
  import memberAdd from './components/add'
  import memberEdit from './components/edit'

  export default {
    name: 'UserManagement',
    components: { memberAdd, memberEdit },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          page: 1,
          perpage: 10,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['add'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm(
          '你确定要取消用户' + row.user_name + '的管理员权限吗',
          null,
          async () => {
            const { data } = await delAdmin({ user_id: row.id })
            if (data.is_success) {
              this.$baseMessage(
                '删除成功',
                'success',
                'vab-hey-message-success'
              )
              await this.fetchData()
            }
          }
        )
      },
      handleSizeChange(val) {
        this.queryForm.perpage = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
          data: { list, total },
        } = await getList(this.queryForm)
        this.list = list
        this.total = total
        this.$forceUpdate()
        this.listLoading = false
      },
    },
  }
</script>
